package top.hg.tool.modules.car.person;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import top.hg.tool.common.exception.CarException;
import top.hg.tool.modules.car.constant.CarConstant;
import top.hg.tool.modules.car.constant.RedisKeyConstant;
import top.hg.tool.modules.car.index.IndexService;

import java.util.Date;
import java.util.List;

/**
 * @Author: skh
 * @Date: 2019/7/16 16:50
 * @Description:
 */
@Service
@Slf4j
public class LotteryPersonService extends ServiceImpl<LotteryPersonDao, LotteryPersonEntity> {

    @Autowired
    private IndexService indexService;

    @Autowired
    private LotteryPersonRepository lotteryPersonRepository;

    @Autowired
    private StringRedisTemplate redisTemplate;

    public Integer saveLotteryPerson(LotteryPersonEntity lotteryPersonEntity) {

        //一个人最多建5个用户
        List<LotteryPersonEntity> lotteryPersonList = lotteryPersonRepository.getPersonListByOpenId(lotteryPersonEntity.getOpenId());
        if (lotteryPersonList.size() >= 5) {
            throw new CarException("一个用户最多新增5个摇号人员");
        }

        lotteryPersonEntity.setStatus(CarConstant.no_query);
        lotteryPersonEntity.setIssue("");
        lotteryPersonEntity.setCreateTime(new Date());
        lotteryPersonEntity.setUpdateTime(new Date());
        Integer id = lotteryPersonRepository.savePerson(lotteryPersonEntity);
        log.info("新增摇号人成功,摇号人id:[{}]", id);

        //删除缓存
        this.deletePersonListCache(lotteryPersonEntity.getOpenId());
        return id;
    }


    public void updateLotteryPerson(LotteryPersonEntity lotteryPersonEntity) {
        if (lotteryPersonEntity.getId() == null) {
            throw new CarException("摇号人id不能为空");
        }

        lotteryPersonEntity.setStatus(CarConstant.no_query);
        lotteryPersonEntity.setIssue("");
        lotteryPersonEntity.setUpdateTime(new Date());
        lotteryPersonRepository.updatePersonById(lotteryPersonEntity);
        log.info("修改摇号人成功,摇号人id:[{}]", lotteryPersonEntity.getId());

        //删除缓存
        this.deletePersonListCache(lotteryPersonEntity.getOpenId());
    }


    public List<LotteryPersonEntity> getLotteryPersonList(String openId) {
        List<LotteryPersonEntity> lotteryPersonEntityList = lotteryPersonRepository.getPersonListByOpenId(openId);
        return lotteryPersonEntityList;
    }

    public void deleteLotteryPerson(Integer id) {
        if (id == null) {
            throw new CarException("摇号人id不能为空");
        }

        LotteryPersonEntity personById = lotteryPersonRepository.getPersonById(id);

        lotteryPersonRepository.deletePersonById(id);
        log.info("删除摇号人成功,摇号人id:[{}]", id);

        this.deletePersonListCache(personById.getOpenId());

    }

    private void deletePersonListCache(String openId) {
        //删除缓存
        try {
            redisTemplate.delete(RedisKeyConstant.car_person_list_openId + openId);
            log.debug("删除缓存");
        } catch (Exception e) {
            log.error("删除缓存异常:[{}]",e.getMessage(),e);
        }
    }


    public LotteryPersonEntity getPersonById(Integer id) {
        if (id == null) {
            throw new CarException("id不能为空");
        }
        return lotteryPersonRepository.getPersonById(id);
    }
}
